package com.wanger.swc.config.myBatisPlus.autoDDL.model;

import com.baomidou.mybatisplus.annotation.TableId;
import com.wanger.swc.util.validation.MyVerifyUtil;
import lombok.Getter;
import lombok.ToString;

import java.lang.reflect.Field;

/**
 * @Author: wanger
 * @Date: 2025/1/20 10:11
 * @Description: 字段
 */
@Getter
@ToString
public class AutoDDLColumn {

    /**
     * java类型
     */
    private final Field javaType;

    /**
     * 数据库字段名
     */
    private final String columnName;

    /**
     * 数据库字段注释
     */
    private final String columnComment;

    public AutoDDLColumn(Field javaType, String columnName, String columnComment) {
        MyVerifyUtil.nullVerify(javaType, "字段类型不能为空！");
        MyVerifyUtil.nullVerify(columnName, "字段类型不能为空！");
        this.javaType = javaType;
        this.columnName = columnName.toLowerCase();
        this.columnComment = columnComment;
    }

    /**
     * 字段名称为id或者加了@TableId注解的都认为是主键
     *
     * @return 是否主键
     */
    public boolean primary() {
        return javaType.getName().equalsIgnoreCase("id") || javaType.getAnnotation(TableId.class) != null;
    }
}
